# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html

import pymysql


class WineSearcherPipeline(object):
    def __init__(self):
        self.db = pymysql.Connect(
            host='127.0.0.1',
            port=3306,
            user='root',
            password='1111',
            db='yuekao',
            charset='utf8'
        )
        self.cur = self.db.cursor()

    def process_item(self, item, spider):
        # c_name = item['c_name']
        # c_href = item['c_href']
        # sql = "insert country values(0,'%s','%s')" % (c_name, c_href)

        # p_href = item['p_href']
        # c_id = item['c_id']
        # sql = "insert winepage values(0,'%s','%s')" % (p_href, c_id)

        region = item['region']
        ex_tax = item['ex_tax']
        producer = item['producer']
        grape = item['grape']
        awards = item['awards']
        notes = item['notes']
        p_id = item['p_id']
        sql = "insert wineinfo values(0,'%s','%s','%s','%s','%s','%s','%s')" % (
            region, ex_tax, producer, grape, awards, notes, p_id)

        self.cur.execute(sql)
        self.db.commit()
        return item
